package controllers;

import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

import javax.imageio.ImageIO;

import view.AbstractGUI;
import client.CliMessage;
import client.IObserve;
import client.ImageSerializer;
import client.SGI;

/**
 * The Class AbstractController.
 */
public abstract class AbstractController implements IObserve 
{
	
	/** The model. */
	protected Object theModel;
	
	/** The view. */
	protected AbstractGUI theView;
	
	/** The back button event listener. */
	protected BackListener backButtonListener;
	
	/** The serialized result set. */
	protected ArrayList<HashMap<String, String>> srs;
	
	/**
	 * Instantiates a new abstract controller.
	 *
	 * @param theView the the view
	 * @param theModel the the model
	 */
	public AbstractController( AbstractGUI theView , Object theModel) 
	{
		super();
		this.theModel = theModel;
		this.theView = theView;
	}
	
	/**
	 * Sets the current observer.
	 *
	 * @param newObserver the new current observer
	 */
	protected void setCurrentObserver(Observer newObserver)
	{
		SGI.clnt.deleteObservers();
		SGI.clnt.addObserver(newObserver);
	}
	
	/**
	 * Sets the back listener.
	 *
	 * @param previousObserver the previous observer
	 * @param newView the new view
	 */
	protected void setBackListener(Observer previousObserver,AbstractGUI newView)
	{
		//add a listener to "back" button in customer complaint window
		backButtonListener = new BackListener(previousObserver,newView,theView);
		newView.addBackActionListener(backButtonListener);
		
		//add a listener to handle window closing
		newView.addWindowListener(backButtonListener);	
		
		//hide old view
		theView.setVisible(false);
	}
	
	/**
	 * Send to server.
	 *
	 * @param request the request
	 */
	protected void sendToServer(String request)
	{
		try 
		{
			SGI.clnt.sendToServer(request);
		} 
		catch (IOException e)
		{
			theView.displayInfoMessage("Error: Server comunication problem","Commuinication Error",0);
			e.printStackTrace();
		}
	}
	
	/* (non-Javadoc)
	 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
	 */
	@Override
	public void update(Observable o, Object arg) 
	{
		String msg = new String(((CliMessage)arg).getMessageFromServer());
		
		if(msg.equals("#OC:Connection established."))
		{
			//Do nothing.
			//maybe needs handling if it is after a reconnection try.
		}
		if( msg.equals("#OC:Connection error.") || msg.equals("#OC:Connection closed." ))
		{
			handleConnectionFail();
			return;
		}
		if( msg.equals("#Server: Action success"))
		{
			handleDBRequestSuccess();
			return;
		}
		if(msg.equals("#Server: Action failed" ))
		{
			handleDBRequestFail() ;
			return;
		}
		if(msg.equals("#Server: Null search result"))
		{	
			handleNullSearchResult();
			return;
		}
		if(msg.equals("#Server: Search result"))
		{ 
		
				handleDBSearchResult((CliMessage)arg);
			
		}
		if(msg.equals("#Server: Image"))
		{
			handlePhoto((CliMessage)arg);
		}

	}
	
	/* (non-Javadoc)
	 * @see client.IObserve#handlePhoto(client.CliMessage)
	 */
	@Override
	public void handlePhoto(CliMessage msgWithSerializedImage)
	{
		
		///
	}

	/* (non-Javadoc)
	 * @see client.IObserve#handleConnectionFail()
	 */
	@Override
	public void handleConnectionFail() 
	{
		//display a failure message to user
		theView.displayInfoMessage("Error: Connection to server faild","Connection Failed",0);
	}
	
	/* (non-Javadoc)
	 * @see client.IObserve#handleDBRequestFail()
	 */
	@Override
	public void handleDBRequestFail() 
	{
		//display a failure message to developer
		theView.displayInfoMessage("Error: SQL quary failed","Database Error",0);
	}
	
}
